package com.xplay.xpocker.temp;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

public class DataGenerator {
    /**
     * 生成代码1
     *
     * @param args
     */
    public static void main(String[] args) {
        //当前项目路径

        // 数据源
        String url = "jdbc:mysql://localhost:3306/xpocker?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&nullCatalogMeansCurrent=true";
        String username = "root";
        String password = "h$IkccO&d8";
        //全局配置
        String projectPath = System.getProperty("user.dir"); //获取项目路径
        String filePath = projectPath + "/src/main/java";  //java下的文件路径
        GlobalConfig config = new GlobalConfig.Builder()
                //作者
                .author("xplay")
                // 生成路径，最好使用绝对路径，window路径是不一样的
                .outputDir(filePath)
                // 文件覆盖
                .fileOverride()
                //设置时间对应类型
                .dateType(DateType.ONLY_DATE).build();
        //包名策略配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                // parent 包
                .parent("com.xplay.xpocker")
                // mapper 包
                .mapper("mapper")
                // mapper 包
                .service("service")
                // mapper 包
                .controller("controller")
                // mapper 包
                .entity("entity")
                // mapper 包
                .xml("mapper").build();
        System.out.println("请输入table列表,例如:sys_user,sys_menu");
        Scanner scanner = new Scanner(System.in);
        String tableList = scanner.next();
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                //设置需要映射的表名
                .addInclude(tableList.split(","))
                //策略开启⼤写命名
                .enableCapitalMode().entityBuilder()
                //添加后缀
                //.formatFileName("%sDO")
                //添加lombock的getter、setter注解
                .enableLombok()
                // 数据库表映射到实体的命名策略
                .columnNaming(NamingStrategy.underline_to_camel).naming(NamingStrategy.underline_to_camel).mapperBuilder()//mapper类添加@Mapper
                //生成基本的SQL片段
                .enableBaseColumnList()
                //生成基本的resultMap
                .enableBaseResultMap().serviceBuilder()
                //添加后缀
                .formatServiceFileName("%sService")
                //使用restController注解
                .controllerBuilder().enableRestStyle().build();

        // 数据源配置
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(url, username, password);
        //dataSourceConfigBuilder.schema("ms_system");
        // 创建代码生成器对象，加载配置
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfigBuilder.build());
        autoGenerator.global(config);
        autoGenerator.packageInfo(packageConfig);
        autoGenerator.strategy(strategyConfig);

        //执行操作
        autoGenerator.execute();
    }
}
